package response;

import java.util.Arrays;

import utils.Conversions;

/**
 * Represents the map transmission header.
 * 		12 bytes in length
 * @author lilywong
 *
 */
public class MapHeader {
	
	public static final int LENGTH = 12;	// 6 fields at 2 bytes each
	
	/* Instance Variables */
	/**
	 *  0	Error
	 *  + 	# of rows
	 */
	int rows;
	/**
	 *  0	Error
	 *  + 	# of columns
	 */
	int cols;
	/**
	 * -1	Error
	 *  + 	# of cells
	 */
	int numHome1;
	int numJail1;
	int numHome2;
	int numJail2;
	
	/* Accessors */
	public int getRows() { return rows; }
	public int getCols() { return cols; }
	public int getNumHome1() { return numHome1; }
	public int getNumJail1() { return numJail1; }
	public int getNumHome2() { return numHome2; }
	public int getNumJail2() { return numJail2; }
	
	/* Constructor */
	public MapHeader( int rows, int cols, int numHome1, int numJail1,
						int numHome2, int numJail2 ) {
		this.rows = rows;
		this.cols = cols;
		this.numHome1 = numHome1;
		this.numJail1 = numJail1;
		this.numHome2 = numHome2;
		this.numJail2 = numJail2;
	}
	
	/* Constructor for client's received response */
	public MapHeader( byte[] headerInBytes ) {
		// grab each 2 byte array representing an integer in network byte order
		byte[] rowsInBytes 		= Arrays.copyOfRange(headerInBytes, 0, 2);
		byte[] colsInBytes 		= Arrays.copyOfRange(headerInBytes, 2, 4);
		byte[] numHome1InBytes 	= Arrays.copyOfRange(headerInBytes, 4, 6);
		byte[] numJail1InBytes 	= Arrays.copyOfRange(headerInBytes, 6, 8);
		byte[] numHome2InBytes 	= Arrays.copyOfRange(headerInBytes, 8, 10);
		byte[] numJail2InBytes 	= Arrays.copyOfRange(headerInBytes, 10, 12);
		
		// convert back to host byte order
		this.rows 		= Conversions.bytesToInt( rowsInBytes );
		this.cols 		= Conversions.bytesToInt( colsInBytes );
		this.numHome1 	= Conversions.bytesToInt( numHome1InBytes );
		this.numJail1 	= Conversions.bytesToInt( numJail1InBytes );
		this.numHome2 	= Conversions.bytesToInt( numHome2InBytes );
		this.numJail2 	= Conversions.bytesToInt( numJail2InBytes );
	}
	
	
	/* Converts a MapHeader to a byte array in network byte order */
	public byte[] toNetworkByteArray() {
		
		// header 
		byte[] headerInBytes = new byte[ LENGTH ];
		
		// convert each field to a 2-byte array in network byte order
		byte[] rowsInBytes 		= Conversions.intToBytes( this.rows );
		byte[] colsInBytes 		= Conversions.intToBytes( this.cols );
		byte[] numHome1InBytes 	= Conversions.intToBytes( this.numHome1 );
		byte[] numJail1InBytes	= Conversions.intToBytes( this.numJail1 );
		byte[] numHome2InBytes 	= Conversions.intToBytes( this.numHome2 );
		byte[] numJail2InBytes 	= Conversions.intToBytes( this.numJail2 );

		// concatenate the arrays into the header in byte form
		System.arraycopy( rowsInBytes, 		0, headerInBytes, 0, 2);
		System.arraycopy( colsInBytes, 		0, headerInBytes, 2, 2);
		System.arraycopy( numHome1InBytes, 	0, headerInBytes, 4, 2);
		System.arraycopy( numJail1InBytes, 	0, headerInBytes, 6, 2);
		System.arraycopy( numHome2InBytes, 	0, headerInBytes, 8, 2);
		System.arraycopy( numJail2InBytes, 	0, headerInBytes, 10, 2);
		
		return headerInBytes;
	}
	
	public void printStringRepresentation() {
		System.out.println( toString() );
	}
	public String toString() {
		return "[" + rows + "," + cols + "," + numHome1 + "," + numJail1 + ","
		+ numHome2 + "," + numJail2 + "]";
	}
}